﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataGeneration
{
    public static class Extensions
    {
        public static StringBuilder AppendInline(this StringBuilder sb, bool inline, string format, params object[] args)
        {
            return sb.AppendFormat(format + (inline ? ", " : "\n"), args);
        }

        private static readonly Random Rnd = new Random();
        public static T Random<T>(this IEnumerable<T> ienum)
        {
            var maxValue = ienum.Count();
            return ienum.Skip(Rnd.Next(maxValue)).FirstOrDefault();
        }

        public static bool IsSubclassOfRawGeneric(this Type toCheck, Type generic)
        {
            if (generic == toCheck)
                return false;
            while (toCheck != null && toCheck != typeof(object))
            {
                var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
                if (generic == cur)
                    return true;
                toCheck = toCheck.BaseType;
            }
            return false;
        }

    }
}